#libraries

library(readxl)
library(tidyverse)
library(hrbrthemes)
library(reshape2)
library(ggpubr)

#files

ÖVP_time <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/ÖVP_time.xlsx")
SPÖ_time <- read_excel("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/data/SPÖ_time.xlsx")

                                ###############
                                ###ÖVP#########
                                ###############

#percentage of MP holding political positions

names(ÖVP_time)

ÖVP_time <- ÖVP_time %>%
  select(GP_dt,"ChamberAgric","EcoChamber","ÖAAB","ÖWB",
         "ÖBB","Youth party","City/District Council","Municipal Council",
         "Federal Council","State Parliament","Federal Government","Regional Government",
         "Major" ,"Party position local/regional","Party position land","Party position national" ,
         counter) 

ÖVP_time <- ÖVP_time %>%
  group_by(GP_dt)  %>%
  summarise_all(sum)

ÖVP_time$"ChamberAgric" <- round((ÖVP_time$"ChamberAgric"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"EcoChamber" <- round((ÖVP_time$"EcoChamber"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"ÖAAB" <- round((ÖVP_time$"ÖAAB"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"ÖWB" <- round((ÖVP_time$"ÖWB"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"ÖBB" <- round((ÖVP_time$"ÖBB"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Youth party" <- round((ÖVP_time$"Youth party"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"City/District Council" <- round((ÖVP_time$"City/District Council"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Municipal Council" <- round((ÖVP_time$"Municipal Council"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Federal Council" <- round((ÖVP_time$"Federal Council"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"State Parliament" <- round((ÖVP_time$"State Parliament"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Federal Government" <- round((ÖVP_time$"Federal Government"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Regional Government" <- round((ÖVP_time$"Regional Government"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Major" <- round((ÖVP_time$"Major"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Party position local/regional" <- round((ÖVP_time$"Party position local/regional"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Party position land" <- round((ÖVP_time$"Party position land"/ÖVP_time$counter)*100,digits=1)
ÖVP_time$"Party position national" <- round((ÖVP_time$"Party position national"/ÖVP_time$counter)*100,digits=1)

ÖVP_time$counter <- NULL

ÖVP_time <-   melt(ÖVP_time, id.vars=c("GP_dt"))

#prepare for visualization

ÖVP_time$GP_dt <- as.factor(ÖVP_time$GP_dt)
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="5"] <- "5 (1945-49)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="6"] <- "6 (1949-53)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="7"] <- "7 (1953-56)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="8"] <- "8 (1956-59)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="9"] <- "9 (1959-62)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="10"] <- "10 (1962-66)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="11"] <- "11 (1966-70)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="12"] <- "12 (1970-71)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="13"] <- "13 (1971-75)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="14"] <- "14 (1975-79)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="15"] <- "15 (1979-83)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="16"] <- "16 (1983-86)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="17"] <- "17 (1986-90)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="18"] <- "18 (1990-94)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="19"] <- "19 (1994-96)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="20"] <- "20 (1996-99)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="21"] <- "21 (1999-02)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="22"] <- "22 (2002-06)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="23"] <- "23 (2006-08)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="24"] <- "24 (2008-13)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="25"] <- "25 (2013-17)"
levels(ÖVP_time$GP_dt)[levels(ÖVP_time$GP_dt)=="26"] <- "26 (2017-19)"

#visualize

ÖVP_plot<- ggplot(ÖVP_time, aes(x=GP_dt,y=variable, fill= value)) +
  geom_tile() +
  scale_fill_distiller(palette = "Spectral", limits = c(min(ÖVP_time$value), max(ÖVP_time$value)))+
  theme_ipsum_rc(base_family = "Hind")+
  theme(axis.text.x  = element_text(angle=90,hjust=1,size=13),
        axis.text.y  = element_text(vjust=0.0,size=13))+
  #theme(legend.title=element_text("% of all MPs "))+
  guides(fill=guide_legend(title="% of all ÖVP MPs"))+
  labs(y = "", x= "legislation", title="ÖVP")+
  theme(plot.title = element_text(face="bold"))+
  geom_text(aes(label = round(value, 1)), color="black", size=3.7) 

ÖVP_plot

                                    ###############
                                    ###SPÖ#########
                                    ###############

#percentage of MP holding political positions

names(SPÖ_time)

SPÖ_time <- SPÖ_time %>%
  select(GP_dt,"ChamberLabour","Trade Union","Youth party","City/District Council",
         "Municipal Council","Federal Council","State Parliament",
         "Federal Government","Regional Government","Major",
         "Party position local/regional","Party position land",
         "Party position national","Party's auxiliary orga.",counter) 

SPÖ_time <- SPÖ_time %>%
  group_by(GP_dt)  %>%
  summarise_all(sum)

SPÖ_time$"ChamberLabour" <- round((SPÖ_time$"ChamberLabour"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Trade Union" <- round((SPÖ_time$"Trade Union"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Youth party" <- round((SPÖ_time$"Youth party"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"City/District Council" <- round((SPÖ_time$"City/District Council"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Municipal Council" <- round((SPÖ_time$"Municipal Council"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Federal Council" <- round((SPÖ_time$"Federal Council"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"State Parliament" <- round((SPÖ_time$"State Parliament"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Federal Government" <- round((SPÖ_time$"Federal Government"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Regional Government" <- round((SPÖ_time$"Regional Government"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Major" <- round((SPÖ_time$"Major"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Party position local/regional" <- round((SPÖ_time$"Party position local/regional"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Party position land" <- round((SPÖ_time$"Party position land"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Party position national" <- round((SPÖ_time$"Party position national"/SPÖ_time$counter)*100,digits=1)
SPÖ_time$"Party's auxiliary orga." <- round((SPÖ_time$"Party's auxiliary orga."/SPÖ_time$counter)*100,digits=1)

SPÖ_time$counter <- NULL

SPÖ_time <-   melt(SPÖ_time, id.vars=c("GP_dt"))

SPÖ_time$GP_dt <- as.factor(SPÖ_time$GP_dt)
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="5"] <- "5 (1945-49)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="6"] <- "6 (1949-53)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="7"] <- "7 (1953-56)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="8"] <- "8 (1956-59)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="9"] <- "9 (1959-62)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="10"] <- "10 (1962-66)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="11"] <- "11 (1966-70)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="12"] <- "12 (1970-71)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="13"] <- "13 (1971-75)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="14"] <- "14 (1975-79)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="15"] <- "15 (1979-83)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="16"] <- "16 (1983-86)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="17"] <- "17 (1986-90)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="18"] <- "18 (1990-94)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="19"] <- "19 (1994-96)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="20"] <- "20 (1996-99)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="21"] <- "21 (1999-02)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="22"] <- "22 (2002-06)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="23"] <- "23 (2006-08)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="24"] <- "24 (2008-13)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="25"] <- "25 (2013-17)"
levels(SPÖ_time$GP_dt)[levels(SPÖ_time$GP_dt)=="26"] <- "26 (2017-19)"


SPÖ_plot<- ggplot(SPÖ_time, aes(x=GP_dt,y=variable, fill= value)) +
  geom_tile() +
  scale_fill_distiller(palette = "Spectral", limits = c(min(SPÖ_time$value), max(SPÖ_time$value)))+
  theme_ipsum_rc(base_family = "Hind")+
  theme(axis.text.x  = element_text(angle=90,hjust=1,size=13),
        axis.text.y  = element_text(vjust=0.0,size=13))+
  #theme(legend.title=element_text("% of all MPs "))+
  guides(fill=guide_legend(title="% of all SPÖ MPs"))+
  labs(y = "", x= "legislation", title="SPÖ")+
  theme(plot.title = element_text(face="bold"))+
  geom_text(aes(label = round(value, 1)), color="black", size=3.7) 

SPÖ_plot

figure_2 <- ggarrange(ÖVP_plot,SPÖ_plot,
                             labels = c("","","",""),
                             ncol = 1, nrow = 2)
figure_2

ggsave("/journal_articles_2021/Pathways_Parliament/submit_SPSR/revise_resubmit/figures/figure2.png", width=15.0,height=20.0,units="in")